﻿using System.Drawing;
using MossmanMill.Properties;
using MossmanMill.DataModel;
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using MossmanMill.View;

namespace MossmanMill
{
    partial class MainForm 
    {

        private Point roomOffset, pinOffset;
        private Font roomFont = new Font("Arial", 8);
        private Rectangle mapRect;
        private DateTime time;
        private Room roomPin, selectedRoom;
        private List<Rack> roomRacks;
        private bool roomSelected;
        private Scrollbar scrollbar;

        public void InitRoom(Room room = null)
        {
            selectedRoom = room;
            level = Level.ROOM;
            rooms = db.GetRooms();
            roomOffset = new Point((viewPanel.Width - 600) / 2, 0);
            mapRect = new Rectangle(roomOffset, new Size(600, 300));
            roomSelected = room != null;
            roomRacks = db.GetRacks();
            scrollbar = new Scrollbar(new Point(mapRect.Left - 40, mapRect.Bottom + 30), 162, roomRacks.Count * 60 + 10);
            menuEditDataInput.Enabled = false;
            menuEditDragDrop.Enabled = false;
        }

        private void DrawRoom(Graphics g)
        {
            g.DrawImage(Resources.MillMap, mapRect);
            foreach (var room in rooms)
            {
                var point = new Point(roomOffset.X + room.Location.X, roomOffset.Y + room.Location.Y);
                g.DrawImage(Resources.MapPoint, new Rectangle(point, new Size(30,30)));
                point.Offset(Resources.MapPoint.Width / 2, 0);
                g.DrawString(room.Name, roomFont, Brushes.Aqua, point);
            }
            int count = 0;

            if (selectedRoom != null) g.DrawString(selectedRoom.Name + " Racks:", Fonts.mediumArial, Brushes.Black, mapRect.Left, mapRect.Bottom + 10);
            g.DrawRectangle(Pens.Black, mapRect.Left - 20, mapRect.Bottom + 30, mapRect.Width + 40, 161);
            bool alt = true;
            foreach (var rack in roomRacks)
            {
                g.FillRectangle(alt ? Brushes.LightGoldenrodYellow : Brushes.Moccasin, mapRect.Left, count * 60 + mapRect.Bottom + 40 - scrollbar.offset, 300, 40);
                g.DrawRectangle(Pens.Black, mapRect.Left, count * 60 + mapRect.Bottom + 40 - scrollbar.offset, 300, 40);
                g.DrawString(rack.Model + rack.Address, roomFont, Brushes.Black, mapRect.Left, count * 60 + mapRect.Bottom + 42 - scrollbar.offset);
                count++;
                alt = !alt;
            }
            scrollbar.Draw(g);
            g.FillRectangle(SystemBrushes.Control, 0, mapRect.Bottom + 192, viewPanel.Width, viewPanel.Height - mapRect.Bottom + 192);
        }


        private void ClickRoom(Point c, int type)
        {

            if (scrollbar.Click(c, type))
            {
                int elapsed = (DateTime.Now - time).Milliseconds;
                if (elapsed > 100)
                {
                    viewPanel.Invalidate(new Rectangle(mapRect.Left - 40, mapRect.Bottom + 30, mapRect.Width + 40, 162)); 
                    time = DateTime.Now;
                }
                return;
            }

            if (mapRect.Contains(c))
            {
                scrollbar.offset = 0;
                //Do map operations
                int index = -1;
                int count = 0;
                foreach (var room in rooms)
                {
                    var p = room.Location;
                    var rect = new Rectangle(p.X + roomOffset.X, p.Y + roomOffset.Y, 30, 30);
                    if (rect.Contains(c))
                    {
                        index = count;
                        break;
                    }
                    count++;
                }
                if (index == -1 && roomPin == null)
                {
                    selectedRoom = null;
                    if (roomSelected && type == 2)
                    {
                        roomSelected = false;
                        roomRacks = db.GetRacks();
                        viewPanel.Invalidate();
                        scrollbar.SetSpan(roomRacks.Count * 60 + 10);
                        return;
                    }
                    return;
                }
                switch (type)
                {
                    case 0: //MouseDown
                        roomPin = rooms[index];
                        pinOffset = new Point((c.X - (roomOffset.X + roomPin.Location.X)) * -1, (c.Y - (roomOffset.Y + roomPin.Location.Y)) * -1);
                        if (selectedRoom != roomPin) roomRacks = db.GetRacks(roomPin.Id);
                        selectedRoom = roomPin;
                        scrollbar.SetSpan(roomRacks.Count * 60 + 10);
                        roomSelected = true;
                        Cursor = Cursors.Hand;
                        time = DateTime.Now;
                        break;
                    case 1: //MouseMove
                        if (roomPin == null) return;
                        c.Offset(-roomOffset.X, -roomOffset.Y);
                        c.Offset(pinOffset);
                        roomPin.SetLocation(c);
                        int elapsed = (DateTime.Now - time).Milliseconds;
                        if (elapsed > 100)
                        {
                            viewPanel.Invalidate(mapRect);
                            time = DateTime.Now;
                        }
                        break;
                    case 2: //MouseUp
                        c.Offset(-roomOffset.X, -roomOffset.Y);
                        c.Offset(pinOffset);
                        roomPin.SetLocation(c);
                        viewPanel.Invalidate();
                        Cursor = DefaultCursor;
                        //Update location in database
                        DatabaseAccess.Instance.UpdateRoom(roomPin);
                        selectedRoom = roomPin;
                        roomPin = null;
                        pinOffset = new Point();
                        break;
                }
                return;
            }

            if (type == 2 && roomRacks != null && c.Y > mapRect.Bottom + 30)
            {
                var i = 0;
                foreach (var rack in roomRacks)
                {
                    var rect = new Rectangle(mapRect.Left, i * 60 + mapRect.Bottom + 40 - scrollbar.offset, 300, 40);
                    if (rect.Contains(c.X, c.Y))
                    {
                        initRack(rack);
                        viewPanel.Invalidate();
                        return;
                    }
                    i++;
                }
            }
        }
    }
}
